import { createRouter, createWebHistory } from 'vue-router'
import { checkAuthToken, isTokenExist, message, getHomePage } from '@/utils/index.js'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
const baseUrl = import.meta.env.VITE_APP_BASE_URL
// 配置 NProgress
NProgress.configure({
  minimum: 0.1, // 最小百分比
  easing: 'ease', // 动画效果
  speed: 500, // 动画速度（毫秒）
  showSpinner: false, // 是否显示加载图标
  trickle: true, // 是否自动增加进度
  trickleSpeed: 200 // 自动增加速度
})
// 定义路由
const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/components/login/index.vue'), // 懒加载
    meta: { noRequiresAuth: true }
  },
  {
    path: '/Websocket',
    name: 'Websocket',
    meta: { noRequiresAuth: true },
    component: () => import('@/views/websocket/index.vue') // 懒加载
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('@/components/Index/index.vue'),
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import('@/views/HomeView/index.vue') // 懒加载
      },
      {
        path: '/about',
        name: 'about',
        component: () => import('@/views/AboutView/index.vue') // 懒加载
      },
      {
        path: '/componentsDemo',
        name: 'componentsDemo',
        children: [
          {
            path: 'routeComponents',
            name: 'routeComponents',
            children: [
              {
                path: 'axios',
                name: 'axios',
                meta: {
                  KeepAlive: true
                },
                component: () => import('@/views/testView/axios/index.vue') // 懒加载
              },
              {
                path: 'loading',
                name: 'loading',
                component: () => import('@/views/testView/loading/index.vue') // 懒加载
              },
              {
                path: 'dialog',
                name: 'dialog',
                component: () => import('@/views/testView/dialog/index.vue') // 懒加载
              },
              {
                path: 'upload',
                name: 'upload',
                component: () => import('@/views/testView/upload/index.vue') // 懒加载
              },
              {
                path: 'virtualTable',
                name: 'virtualTable',
                component: () => import('@/views/testView/virtualTable/index.vue') // 懒加载
              },
              {
                path: 'websocket',
                name: 'websocket2',
                component: () => import('@/views/testView/websocket/index.vue') // 懒加载
              }
            ]
          }
        ]
      }
    ]
  }
]

// 创建路由器实例
const router = createRouter({
  history: createWebHistory(baseUrl),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 在路由切换前启动进度条
  NProgress.start()
  if (to.meta.noRequiresAuth) {
    next() // 继续导航
  } else if (!isTokenExist()) {
    next('/login') // 如果需要认证但用户没有有效的token，重定向到登录页面
  } else if (!checkAuthToken()) {
    message.warning('身份验证过期，请重新登陆！')
    next('/login') // 身份验证过期
  } else if (to.path === '/') {
    next(getHomePage().path)
  } else {
    next() // 继续导航
  }
})

// 在路由切换完成后结束进度条
router.afterEach(() => {
  NProgress.done()
})
export default router
